#!/usr/bin/env python

'''
__author__ = 'Talos Tse'
__email__ = 'talos.tse@gmail.com'
Created on '2013/11/06 11:09'
'''


import multiprocessing
import time
import os
import hashlib

rootdir = '.'
filelist = []
worker_count = 4


def getlist_file( rootdir ):
    for parent, dirnames, filenames in os.walk(rootdir):
        for filename in filenames:
            filelist.append(os.path.join(parent, filename))
    return filelist


def ttse_calcMD5(filepath):
    with open(filepath,'rb') as f:
        content = f.read()
        f.close()
        md5obj = hashlib.md5()
        md5obj.update(content)
        hash = md5obj.hexdigest()
        #print(hash)
        print hash.upper()

if __name__ == "__main__":
    filelist = getlist_file(rootdir)
    filelist_count = len(filelist)
    worker_job_count = (filelist_count/worker_count)


    if (filelist_count  % worker_count) > 0:
        dojob_count = worker_job_count + 1

    p = multiprocessing.Pool(processes=worker_count)
    result = []
    for i in xrange(dojob_count):
        filepath = filelist[ (i*worker_job_count) : (i*worker_job_count+worker_job_count) ]
        for file in filepath:
            result.append(p.apply_async(ttse_calcMD5, (file, )))

    p.close()
    p.join()
    for res in result:
        if res.get():
            print res.get()



    for i in filelist:
        print i
    print "sub-process done"

